package com.df.ez_game.server.broker;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import io.scalecube.cluster.metadata.MetadataCodec;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class JsonMetaCodec implements MetadataCodec {
    @Override
    public Object deserialize(ByteBuffer buffer) {

        String str = new String(buffer.array(), buffer.position(), buffer.remaining(), StandardCharsets.UTF_8);
        ClusterNode.ClusterNodeMeta clusterNodeMeta = JSON.parseObject(str, ClusterNode.ClusterNodeMeta.class);
        return clusterNodeMeta;
    }

    @Override
    public ByteBuffer serialize(Object o) {
        String jsonString = JSON.toJSONString(o);
        byte[] bytes = jsonString.getBytes(StandardCharsets.UTF_8);
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        buffer.put(bytes);
        return buffer;
    }
}
